package org.hanbo.code.repository.persistence.entity;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.hanbo.code.repository.persistence.entity.CodeSnippet;
import org.hanbo.code.repository.persistence.DAOBase;

import com.google.appengine.api.datastore.Key;

public class CodeSnippetDAO extends DAOBase
{
	public CodeSnippetDAO()
	{ }
	
	public Key persistCodeSnippet(CodeSnippet snippet)
	{
		if (super.isPersistenceManagerAcquired())
		{
			if (snippet.isCodeSnippetDataValid())
			{
				CodeSnippet objPersistent
					= super.getPersistenceManager()
						.<CodeSnippet>makePersistent(snippet);
				return objPersistent.getId();
			}
		}
		
		throw new IllegalArgumentException("The input code snippet is invalid.");
	}
	
	public CodeSnippet getCodeSnippetById(Key snippetId)
	{
		if (super.isPersistenceManagerAcquired())
		{
			CodeSnippet snippet
				= super
					.getPersistenceManager()
					.getObjectById(CodeSnippet.class, snippetId);
			return snippet;
		}
		
		throw new IllegalArgumentException("Invalid PersistenceManager for getCodeSnippetById");
	}

	public void deleteCodeSnippetById(Key snippetId)
	{
		if (super.isPersistenceManagerAcquired())
		{
			CodeSnippet snippet
				= super
					.getPersistenceManager()
					.getObjectById(CodeSnippet.class, snippetId);
			
			super.getPersistenceManager().deletePersistent(snippet);
			return;
		}
		
		throw new IllegalArgumentException("Invalid PersistenceManager for getCodeSnippetById");
	}
	
	public void updateCodeSnippetById(Key snippetId, CodeSnippet updateInfo)
	{
		if (super.isPersistenceManagerAcquired())
		{
			CodeSnippet snippet
				= super
					.getPersistenceManager()
					.getObjectById(CodeSnippet.class, snippetId);
			snippet.setAvailabilityToPublic(updateInfo.isAvailableTpPublic());
			snippet.setCodeComments(updateInfo.getCodeComments());
			snippet.setCodeSnippet(updateInfo.getCodeSnippet());
			snippet.setKeywords(updateInfo.getKeywords());
			snippet.setLanguageType(updateInfo.getLanguageType());
			snippet.setReferenceLink(updateInfo.getReferenceLink());
			snippet.setRelatedMethods(updateInfo.getRelatedMethods());
			snippet.setSubject(updateInfo.getSubject());
		}
		
		throw new IllegalArgumentException("Invalid PersistenceManager for getCodeSnippetById");
	}
	
	public List<CodeSnippet> findAllSnippetsByAuthorEmail(
		String authorEmail, int recordStartIdx, int numRecordsReturned
	)
	{
		if (super.isPersistenceManagerAcquired())
		{
			PersistenceManager pm = super.getPersistenceManager();
			Query query = pm.newQuery(CodeSnippet.class);
			query.setFilter("authorEmail == authorEmailParam");
			query.declareParameters("String authorEmailParam");
			query.setOrdering("createDate desc");
			query.setRange(recordStartIdx, numRecordsReturned + recordStartIdx);
			
			@SuppressWarnings("unchecked")
			List<CodeSnippet> results
				= (List<CodeSnippet>) query.execute(authorEmail);
			
			return results;
		}
		
		throw new IllegalArgumentException("Invalid PersistenceManager for findAllSnippetsByAuthorEmail");		
	}

	public List<CodeSnippet> findAllPublicViewableSnippets(
		int recordStartIdx, int numRecordsReturned
	)
	{
		if (super.isPersistenceManagerAcquired())
		{
			PersistenceManager pm = super.getPersistenceManager();
			Query query = pm.newQuery(CodeSnippet.class);
			query.setFilter("availableToPublic == availableToPublicParam");
			query.declareParameters("boolean availableToPublicParam");
			query.setOrdering("createDate desc");
			query.setRange(recordStartIdx, numRecordsReturned + recordStartIdx);
			
			@SuppressWarnings("unchecked")
			List<CodeSnippet> results
				= (List<CodeSnippet>) query.execute(true);
			
			return results;
		}
		
		throw new IllegalArgumentException("Invalid PersistenceManager for findAllPublicViewableSnippets");		
	}
}
